<?php
namespace app\api\controller;

use app\common\model as M;

/**
 *
 */
class Recomand extends \app\api\ApiBase
{
    public $model = '\app\common\model\Recomand';

    /**
     * 图标推荐位
     * @Author   FW
     * @DateTime 2024-03-18
     * @Email    123763712@qq.com
     * @return   [type]           [description]
     */
    public function icons()
    {
        return $this->returnSuccess($this->model::where(['status'=>1,'type'=>1])->order(sprintf('sort %s',$this->model::DEFAULT_SORT_RULE))->select());
    }

    /**
     * 文字推荐位
     * @Author   FW
     * @DateTime 2024-03-18
     * @Email    123763712@qq.com
     * @return   [type]           [description]
     */
    public function texts()
    {
        $recomandTexts = $this->model::where(['status'=>1,'type'=>2])->order(sprintf('sort %s',$this->model::DEFAULT_SORT_RULE))->select();
        if ( !empty($recomandTexts) ) {
            $recomandTexts = $recomandTexts->toArray();

            foreach ($recomandTexts as $key => &$text) {
                $text['routes'] = M\RelRecomandRoutes::alias('rel')->field('r.id,r.route_name,r.cover,r.price,r.prime_price,r.label')->join('__ROUTES__ r','rel.route_id = r.id')->where('r.status',1)->where('rel.recomand_id',$text['id'])->select()->toArray();

                if ( empty($text['routes']) ) {
                    unset($recomandTexts[$key]);
                    continue;
                }

                foreach ( $text['routes'] as &$v ) {
                    $v['label'] = json_decode($v['label']);
                }
            }
        }
        return $this->returnSuccess($recomandTexts);
    }

    /**
     * 线路列表
     * @Author   FW
     * @DateTime 2024-03-13
     * @Email    123763712@qq.com
     * @return   [type]           [description]
     */
    public function routes($recomand_id)
    {
        return $this->returnSuccess(M\RelRecomandRoutes::routes($recomand_id , $this->page , $this->limit));
    }

}//Class End